package com.itheima.prince.smartpeking.fragment;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.itheima.prince.smartpeking.R;
import com.itheima.prince.smartpeking.activity.MainActivity;
import com.itheima.prince.smartpeking.base.BaseFragment;
import com.itheima.prince.smartpeking.base.BasePager;
import com.itheima.prince.smartpeking.impl.GovPager;
import com.itheima.prince.smartpeking.impl.HomePager;
import com.itheima.prince.smartpeking.impl.NewsPager;
import com.itheima.prince.smartpeking.impl.ServerPager;
import com.itheima.prince.smartpeking.impl.SettingPager;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Prince on 2016/10/19.
 */
public class ContentFragment extends BaseFragment {

    private ViewPager mviewPager;
    List<BasePager> pagers = new ArrayList<>();
    private RadioGroup radioGroup;

    @Override
    protected View initView() {
        View inflate = View.inflate(mactivity, R.layout.fragment_content, null);
        mviewPager = (ViewPager) inflate.findViewById(R.id.vp_main_content);
        radioGroup = (RadioGroup) inflate.findViewById(R.id.rg_main_content);
        return inflate;
    }

    @Override
    public void initData() {
        pagers.add(new HomePager(mactivity));
        pagers.add(new NewsPager(mactivity));
        pagers.add(new ServerPager(mactivity));
        pagers.add(new GovPager(mactivity));
        pagers.add(new SettingPager(mactivity));



        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i){
                    case R.id.rb_home:
                        mviewPager.setCurrentItem(0,false);
                        enableopenmenu(false);
                        break;
                    case R.id.rb_news:
                        mviewPager.setCurrentItem(1,false);
                        enableopenmenu(true);
                        break;
                    case R.id.rb_server:
                        mviewPager.setCurrentItem(2,false);
                        enableopenmenu(true);
                        break;
                    case R.id.rb_gov:
                        mviewPager.setCurrentItem(3,false);
                        enableopenmenu(true);
                        break;
                    case R.id.rb_setting:
                        mviewPager.setCurrentItem(4,false);
                        enableopenmenu(false);
                        break;
                }
            }


        });

        mviewPager.setAdapter(new MyPagerAdapter() {
        });
        pagers.get(0).initdata();
        enableopenmenu(false);
        mviewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                BasePager pager = pagers.get(position);
                pager.initdata();
//                if (position==1){
//                    NewsPager newsPager = (NewsPager) pager;
//                   MainActivity mainActivity= (MainActivity) mactivity;
//                    LeftFragment leftFragment = mainActivity.getLeftFragment();
//                    newsPager.switchMenuPager(leftFragment.currentIndex);
//                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }



    private class MyPagerAdapter extends PagerAdapter {
        @Override
        public int getCount() {
            return pagers.size();
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            BasePager pager = pagers.get(position);
            View view = pager.rootView;
            container.addView(view);//知道你也忘了
            return view;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }
    }

    private void enableopenmenu(boolean enable){
        MainActivity mainActivity = (MainActivity) mactivity;
        SlidingMenu slidingMenu = mainActivity.getSlidingMenu();
        if (enable){
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        }else {
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
        }
    }

    public NewsPager getNewsPager(){
        return (NewsPager) pagers.get(1);
    }
}
